<?php
$count = count($comfort);
$tmp = array();
foreach ($comfort as $k => $item) {
    if ($k < (count($comfort) / 2)) {
        $tmp[0][] = $item;
    } else {
        $tmp[1][] = $item;
    }
}
$comfort = $tmp;
?>

<div id="content"><div class="container">
        <div id="main">
            <div class="row">
                <div class="span9">
                    <h1 class="page-header"><?= $model->getAddress(); ?></h1>

                    <div class="carousel property">
                        <div class="preview">
                            <?= ImageResizer::getThumb($model->img0, 'data/imgs/flat/' . $model->id . '/', $model->img0, 870, 420) ?>
                        </div><!-- /.preview -->

                        <div class="content">

                            <a class="carousel-prev" href="#">Previous</a>
                            <a class="carousel-next" href="#">Next</a>
                            <ul>
                                <?php for ($i = 0; $i < 8; $i++) { ?>
                                    <li class="active">
                                        <?php
                                        if (!empty($model->{'img' . $i})) {
                                            echo ImageResizer::getThumb($model->{'img' . $i}, 'data/imgs/flat/' . $model->id . '/', $model->{'img' . $i}, 870, 420);
                                        }
                                        ?>
                                    </li>
                                <?php } ?>
                            </ul>
                        </div>
                        <!-- /.content -->
                    </div>
                    <!-- /.carousel -->

                    <div class="property-detail">
                          
                        <div class="pull-left overview" style="width: 300px;">
                            <div class="row">
                                <div class="span3">
                                    <table style="width: 300px;">
                                        <tr>
                                            <th>Тип:</th>
                                            <td><?= $model->getTypeFlatText(); ?></td>
                                        </tr>
                                        <tr>
                                            <th>Ремонт:</th>
                                            <td><?= $model->getRepairText(); ?></td>
                                        </tr>
                                        <tr>
                                            <th>Гостей:</th>
                                            <td><?= $model->guests; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Размещение:</th>
                                            <td><?= $model->sleeper; ?></td>
                                        </tr>
                                        <tr>
                                            <th>&nbsp;</th>
                                            <td>&nbsp;</td>
                                        </tr>
                                        <tr>
                                            <th><h2 style="margin: 0;">Цена:</h2></th>
                                        <td><h2 style="margin: 0;"><?= $model->price; ?> грн./сутки</h2></td>
                                        </tr>
                                    </table>
                                </div>
                                <!-- /.span2 -->
                            </div>
                            <!-- /.row -->
                        </div>
                        
                        
                        
                        <div class="row">

                            <?php foreach ($comfort as $items) { ?>
                                <ul class="span2">
                                    <?php foreach ($items as $item) { ?>

                                        <?php
                                        $checked = false;
                                        foreach ($model->comforts as $comf) {
                                            if ($comf->id == $item->id) {
                                                $checked = true;
                                            }
                                        }
                                        ?>
                                        <li class="<?= $checked ? "checked" : "plain"; ?>">
                                            <?= $item->title; ?>
                                        </li>
                                    <?php } ?>
                                </ul>
                            <?php } ?>
                        </div>

                        <?php if (!empty($model->description)) { ?>
                            <h2 style="margin: 0;">
                                Описание
                                <div style=" float: right;" class="yashare-auto-init" data-yashareL10n="ru" data-yashareType="none" data-yashareQuickServices="yaru,vkontakte,facebook,twitter,odnoklassniki,moimir,lj,gplus"></div>
                        
                            </h2>
                            <p><?= nl2br($model->description, false); ?></p>
                        <?php } ?>

                        <?php if (!empty($model->infrastructure)) { ?>
                            <h2 style="margin: 0;">Инфраструктурa</h2>

                            <p><?= nl2br($model->infrastructure, false); ?></p>
                        <?php } ?>

                        <?php if (!empty($model->furniture)) { ?>
                            <h2 style="margin: 0;">Мебель</h2>
                            <p><?= nl2br($model->furniture, false); ?></p>
                        <?php } ?>

                        <h2>Карта</h2>
                        
                        <div id="property-map"></div><!-- /#property-map -->
                        
                        <script type="text/javascript">VK.init({apiId: 3867306, onlyWidgets: true});</script>
                        <!-- Put this div tag to the place, where the Comments block will be -->
                        <div style=" padding-top: 20px;" id="vk_comments"></div>
                        <script type="text/javascript">
                        VK.Widgets.Comments("vk_comments", {limit: 10, width: "auto", attach: "*"});
                        </script>
                        
                    </div>

                </div>
                <div class="sidebar span3">
                    
                    <?php $this->widget('application.widgets.front.ContactAgentWidget', array(
                        'flat' =>  $model,
                        'privateMessage' => $privateMessage,
                    )); ?>
                    
                    <?php $this->widget('application.widgets.front.FlatUserWidget', array(
                        'flatId' =>  $model->id,
                    )); ?>
                    
                    <?php $this->widget('application.widgets.front.FlatNearbyWidget', array(
                        'flatId' =>  $model->id,
                    )); ?>
                    
                </div>
            </div>
        </div>
    </div>
</div>

<?php 
$location = "['" . $model->сity->url . "/" . $model->id . "',
    " . $model->lat . ",
        " . $model->lng . ", 
            '" . ImageResizer::getThumb($model->img0, 'data/imgs/flat/' . $model->id . '/', $model->img0, 100, 74) . "',
                '" . $model->street . " " . $model->home . "',
                    '" . $model->getTypeFlatText() . "', 
                        '" . $model->guests . "',
                            '" . $model->price . "']";
?>
<script language="JavaScript">
    $(document).ready(function() {
        InitMap();
    });

    function LoadMapProperty() {
        var locations = new Array(
                [<?= $model->lat; ?>, <?= $model->lng; ?>]
                );
        var markers = new Array();
        var mapOptions = {
            center: new google.maps.LatLng(<?= $model->lat; ?>, <?= $model->lng; ?>),
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false
        };

        var map = new google.maps.Map(document.getElementById('property-map'), mapOptions);

        $.each(locations, function(index, location) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(location[0], location[1]),
                map: map,
                icon: 'http://html.realia.byaviators.com/assets/img/marker-transparent.png'
            });

            var myOptions = {
                content: '<div class="infobox">\n\
<div class="image"><a href="<?= $model->сity->url . "/" . $model->id; ?>"><?= ImageResizer::getThumb($model->img0, 'data/imgs/flat/' . $model->id . '/', $model->img0, 100, 74);?></a></div>\n\
<div class="title"><a href="<?= $model->сity->url . "/" . $model->id; ?>"><?= $model->street . " " . $model->home;?></a></div>\n\
<div class="area"><span class="key">Тип&nbsp;:&nbsp;</span><span class="value">&nbsp;<?= $model->getTypeFlatText(); ?></div>\n\
<div class="area"><span class="key">Гостей&nbsp;:&nbsp;</span><span class="value">&nbsp;<?= $model->guests; ?></div>\n\
<div class="price">Цена : <?= $model->price; ?> грн.</div>\n\
<div class="link"><a href="<?= $model->сity->url . "/" . $model->id; ?>">Смотреть</a></div>\n\
</div>',
                disableAutoPan: false,
                maxWidth: 0,
                pixelOffset: new google.maps.Size(-146, -190),
                zIndex: null,
                closeBoxURL: "",
                infoBoxClearance: new google.maps.Size(1, 1),
                position: new google.maps.LatLng(location[0], location[1]),
                isHidden: false,
                pane: "floatPane",
                enableEventPropagation: false
            };
            marker.infobox = new InfoBox(myOptions);
            marker.infobox.isOpen = false;

            var myOptions = {
                draggable: true,
                content: '<div class="marker"><div class="marker-inner"></div></div>',
                disableAutoPan: true,
                pixelOffset: new google.maps.Size(-21, -58),
                position: new google.maps.LatLng(location[0], location[1]),
                closeBoxURL: "",
                isHidden: false,
                // pane: "mapPane",
                enableEventPropagation: true
            };
            marker.marker = new InfoBox(myOptions);
            marker.marker.open(map, marker);
            markers.push(marker);

            google.maps.event.addListener(marker, "click", function(e) {
                var curMarker = this;

                $.each(markers, function(index, marker) {
                    // if marker is not the clicked marker, close the marker
                    if (marker !== curMarker) {
                        marker.infobox.close();
                        marker.infobox.isOpen = false;
                    }
                });


                if (curMarker.infobox.isOpen === false) {
                    curMarker.infobox.open(map, this);
                    curMarker.infobox.isOpen = true;
                    map.panTo(curMarker.getPosition());
                } else {
                    curMarker.infobox.close();
                    curMarker.infobox.isOpen = false;
                }

            });
        });
    }

    function InitMap() {
        google.maps.event.addDomListener(window, 'load', LoadMapProperty);
    }

</script>